import Scanner from "./Scanner.js"
import nextTokens from "./nextTokens.js"

export default function parseTemplateToTokens(str){
    var tokens = [];
    var scanner = new Scanner(str);
    var word;
    while(scanner.pos != str.length){
        word = scanner.scanUntil("{{")
        if(word != ''){
            //去空格 尖括号内空格不去
            let _word = "";
            let isIn = false;
            for (let i = 0; i < word.length; i++) {
                if(word[i] ==="<"){
                    isIn = true;
                }
                if(word[i]=== ">"){
                    isIn = false;
                }
                if(word[i] !==" "){
                    _word+=word[i];
                }else{
                    if(isIn){
                        _word+=" "
                    }
                }
            }
           
            tokens.push(['text',_word])
        }
        scanner.scan('{{')

        word = scanner.scanUntil("}}");
        if(word != ''){
            if(word[0]=="#"){
                tokens.push(['#',word.substring(1)])
            }
            else if(word[0]=="/"){
                tokens.push(['/',word.substring(1)])
            } else{
                tokens.push(['name',word])
            }
        }
        scanner.scan('}}')
    }
    
    // return tokens;
    return  nextTokens(tokens);

}